using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ContentListView : AdminBaseUserControl
    {
        public event EventHandler EditContent;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindContent(object sender, EventArgs e)
        {
            PageAddEdit ucPageAddEdit = (PageAddEdit)sender;
            //ContentList contents = ucPageAddEdit.Contents;
            //BindContent(contents);
            Session["PageID"] = ucPageAddEdit.PageID;
            Session["CategoryID"] = null;
        }

        public void DoBindCategoryContent(object sender, EventArgs e)
        {
            CategoryAddEdit ucCategoryAddEdit = (CategoryAddEdit)sender;
            ContentList contents = ucCategoryAddEdit.Contents;
            //BindContent(contents);
            Session["CategoryID"] = ucCategoryAddEdit.CategoryID;
            Session["PageID"] = null;
        }

        public void BindContent(ContentList contents)
        {
            IList<IContent> list = new List<IContent>();
            foreach (IContent content in contents)
             {
                 list.Add(content);
             }
                    gvContent.DataSource = list;
                    gvContent.DataBind();
                ViewState["ContentID"] = null;
            
           
        }

        public void DoDelete(object sender, EventArgs e)        
        {
           
                //if (ViewState["ContentID"] != null) ContentController.DeleteContent(Convert.ToInt64(ViewState["ContentID"]));
                //BindContent(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));

        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
            if (Session["PageID"] != null)
            {
                IPage page = new IGlobalist.Cms.Model.Page();
                page = PageController.GetPageCurrent(Convert.ToInt64(Session["PageID"]));
                BindContent(page.ContentList);
            }
            if (Session["CategoryID"] != null)
            {
                IContentCategory category = CategoryController.GetCategory(Convert.ToInt64(Session["CategoryID"]));
                category.ContentList = ContentController.GetContentListLelateToObjectID(category.ID,CmsObjectType.ContentCategory);
                BindContent(category.ContentList);
            }

        }

        public string SelectedID
        {
            get { return Convert.ToString(ViewState["ContentID"]); }
            set { ViewState["ContentID"] = value; }
        }

        protected void gvContent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                long ID = Convert.ToInt64(e.CommandArgument);
                ViewState["ContentID"] = ID;
                if (e.CommandName == "editContent")
                {
                    if (EditContent != null)
                    {
                        EditContent(this, new EventArgs());
                        
                    }
                }
                if (e.CommandName == "delContent")
                {
                    ContentController.DeleteContent(ID);
                    if (Session["PageID"] != null)
                    {
                        IPage page = new IGlobalist.Cms.Model.Page();
                        page = PageController.GetPageCurrent(Convert.ToInt64(Session["PageID"]));
                        BindContent(page.ContentList);
                    }
                    if (Session["CategoryID"] != null)
                    {
                        IContentCategory category = CategoryController.GetCategory(Convert.ToInt64(Session["CategoryID"]));
                        category.ContentList = ContentController.GetContentListLelateToObjectID(category.ID,CmsObjectType.ContentCategory);
                        BindContent(category.ContentList);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("[[Error]]", ex);
            }
        }

        protected void gvContent_PageIndexChanged(object sender, EventArgs e)
        {
            gvContent.PageIndex = gvContent.PageIndex + 1;
        }
    }
}